﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TUR03
    {
        #region ==dwFN===========
        public static readonly DependencyProperty dwFNProperty = DependencyProperty.Register("dwFN",
            typeof(DataRow), typeof(TUR03),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwFNChanged));
        public DataRow dwFN
        {
            get
            {
                return (DataRow)GetValue(dwFNProperty);
            }
            set
            {
                SetValue(dwFNProperty, value);
                OnFUNCTIONChg(value);
            }
        }
        private static void OndwFNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TUR03;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwFN = row;
            }
        }
        #endregion
        public TUR03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_USERRIGHT();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = ""; 
            }
            else
            {
                KEY = dwMaster["ROWNID"].ToString(); 
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                CONTROLS = "";
                IsNew = true;
               // return;
            }
            dwData = TBMain.NewRow();
            ((VICC_USERRIGHT)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
            dwData["ROWNID"] = LFD;
            dwData["USERID"] = USERID;
            dwData["COMPANY"] = app.company;
            
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
            IsNew = false;
            CONTROLS = "";
        }
        public void Insert(DataRow dwUR)
        {
            ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(dwUR, 1);
            ((VICC_USERRIGHT)MV).InsertICC_USERRIGHT(dwUR, app.company);
            TBMain.Rows.InsertAt(dwUR, 0);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_USERRIGHT)MV).UpdateICC_USERRIGHT(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
            dwData["ROWNID"] = LFD;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(dwMaster, 1);
            string filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void OnPROGChg(string value)
        {

            cvMaster.CustomFilter = "PROG='"+value.PadRight(80)+"'";
            base.OnPROGChg(value);
        }
        public override void OnUSERIDChg(string value)
        {
            if (USERID.Length < 1)
                return;
            dwData = TBMain.NewRow();
            dwData["COMPANY"] = app.company;
            dwData["USERID"] = value.PadRight(8);
            SResult = ((VICC_USERRIGHT)MV).QueryICC_USERRIGHT(dwData, 3);
            MergeData(SResult, false); 
        }
        public override void OnCONTROLSChg(string value)
        {
            if (value.Length < 120)
                return;
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            ((VICC_USERRIGHT)MV).PROG = value.Substring(0, 80);
            ((VICC_USERRIGHT)MV).CONTROLS = value.Substring(80, 40);
            ((VICC_USERRIGHT)MV).REMARK = value.Substring(120);            
            base.OnCONTROLSChg(value);
        }
    }
}
